package cn.easybuy.dao.daoimpl;

import cn.easybuy.dao.UserDao;
import cn.easybuy.entity.User;
import cn.easybuy.utils.Utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @Override
    public int addUser(User user) {
        String sql="insert into easybuy_user(loginName,userName,password,sex,identityCode,email,mobile,type) value(?,?,?,?,?,?,?,?);";
        return Utils.execute(sql,user.getLoginName(),user.getUserName(),user.getPassword(),user.getSex(),user.getIdentityCode(),user.getEmail(),user.getMobile(),
                user.getType());
    }

    /**
     * 以id删除用户
     *
     * @param id
     * @return
     */
    @Override
    public int deleteUser(Long id) {
        String sql="delete from easybuy_user where id=?;";
        return Utils.execute(sql,id);
    }

    @Override
    public List<User> getAllUser() {
        return null;
    }

    @Override
    public User getByName(String name) {
        return null;
    }

    @Override
    public User getById(Long id) {
        return null;
    }

    /**
     * 以账户名删除用户
     *
     * @param name
     * @return
     */
    @Override
    public int deleteUser(String name) {
        String sql="delete from easybuy_user where userName=?;";
        return Utils.execute(sql,name);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user) {
        String sql="update easybuy_user loginName=?,userName=?,password=?,sex=?,identityCode=?,email=?,mobile=?,type=? where id=?;";
        return Utils.execute(sql,user.getLoginName(),user.getUserName(),user.getPassword(),user.getSex(),user.getIdentityCode(),user.getEmail(),user.getMobile(),user.getType(),user.getId());
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<User> selectAllUser() {
        String sql="select * from easybuy_user;";
        List<User> list=new ArrayList<>();
        Connection connection=null;
        ResultSet rs=null;
        try {
            connection= Utils.getConnection();
            rs= Utils.select(sql,connection);
            if (rs!=null){
                while (rs.next()){
                    list.add(new User(rs.getLong("id"),rs.getString("loginName"),rs.getString("userName"),rs.getString("password"),rs.getLong("sex"),rs.getString("identityCode"),rs.getString("email"),rs.getString("mobile"),rs.getLong("type")));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            Utils.closeAll(connection,null,rs);
        }
        return list;
    }

    /**
     * 查询指定登录名的所有用户
     *
     * @param loginName
     * @return
     */
    @Override
    public List<User> selectAllUser(String loginName) {
        String sql="select * from easybuy_user where loginName=?;";
        List<User> list=new ArrayList<>();
        Connection connection=null;
        ResultSet rs=null;
        try {
            connection= Utils.getConnection();
            rs= Utils.select(sql,connection,loginName);
            if (rs!=null){
                while (rs.next()){
                    list.add(new User(rs.getLong("id"),rs.getString("loginName"),rs.getString("userName"),rs.getString("password"),rs.getLong("sex"),rs.getString("identityCode"),rs.getString("email"),rs.getString("mobile"),rs.getLong("type")));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            Utils.closeAll(connection,null,rs);
        }
        return list;
    }

    /**
     * 查询指定id的用户
     *
     * @param id
     * @return
     */
    @Override
    public User selectUser(Long id) {
        String sql="select * from easybuy_user where id=?;";
        Connection connection=null;
        ResultSet rs=null;
        try {
            connection= Utils.getConnection();
            rs= Utils.select(sql,connection,id);
            if (rs!=null){
                while (rs.next()){
                    return new User(rs.getLong("id"),rs.getString("loginName"),rs.getString("userName"),rs.getString("password"),rs.getLong("sex"),rs.getString("identityCode"),rs.getString("email"),rs.getString("mobile"),rs.getLong("type"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            Utils.closeAll(connection,null,rs);
        }
        return null;
    }

    /**
     * 查询指定用户名的用户
     *
     * @param name
     * @return
     */
    @Override
    public User selectUser(String name) {
        String sql="select * from easybuy_user where userName=?;";
        Connection connection=null;
        ResultSet rs=null;
        try {
            connection= Utils.getConnection();
            rs= Utils.select(sql,connection,name);
            if (rs!=null){
                while (rs.next()){
                    return new User(rs.getLong("id"),rs.getString("loginName"),rs.getString("userName"),rs.getString("password"),rs.getLong("sex"),rs.getString("identityCode"),rs.getString("email"),rs.getString("mobile"),rs.getLong("type"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
           Utils.closeAll(connection,null,rs);
        }
        return null;
    }
}
